package com.iengoo.inductionms.common;

import com.iengoo.inductionms.entity.Employee;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class AccountExcel {
    public static void export(String path, String name, List<Employee> employees) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet();
        sheet.setDefaultColumnWidth(20);//自定义设置列宽和行高
        sheet.setDefaultRowHeightInPoints(20);

        HSSFRow row = sheet.createRow(0);
        /**
         * 字体设置
         */
        HSSFFont font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 10);//设置字体大小
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗

        /**
         * 单元格背景为黄色
         */
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());//设置背景色
        cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

        cellStyle.setAlignment(CellStyle.ALIGN_LEFT);//居中
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);//下边框
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);//左边框
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);//上边框
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);//右边框
        cellStyle.setFont(font);

        /**
         * 单元格背景为浅绿色
         */
        CellStyle cellStyle02 = wb.createCellStyle();
        cellStyle02.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());//设置背景色
        cellStyle02.setFillPattern(CellStyle.SOLID_FOREGROUND);

        cellStyle02.setAlignment(CellStyle.ALIGN_LEFT);//居中
        cellStyle02.setBorderBottom(CellStyle.BORDER_THIN);//下边框
        cellStyle02.setBorderLeft(CellStyle.BORDER_THIN);//左边框
        cellStyle02.setBorderRight(CellStyle.BORDER_THIN);//上边框
        cellStyle02.setBorderTop(CellStyle.BORDER_THIN);//右边框
        cellStyle02.setFont(font);

        Cell cell = row.createCell(0);
        cell.setCellStyle(cellStyle02);
        cell.setCellValue("序号");

        Cell cell1 = row.createCell(1);
        cell1.setCellStyle(cellStyle);
        cell1.setCellValue("姓名");

        Cell cell2 = row.createCell(2);
        cell2.setCellStyle(cellStyle);
        cell2.setCellValue("工号");

        Cell cell3 = row.createCell(3);
        cell3.setCellStyle(cellStyle);
        cell3.setCellValue("部门");

        Cell cell4 = row.createCell(4);
        cell4.setCellStyle(cellStyle);
        cell4.setCellValue("职务");

        Cell cell5 = row.createCell(5);
        cell5.setCellStyle(cellStyle);
        cell5.setCellValue("手机号");

        Cell cell6 = row.createCell(6);
        cell6.setCellStyle(cellStyle02);
        cell6.setCellValue("短号");

        Cell cell7 = row.createCell(7);
        cell7.setCellStyle(cellStyle);
        cell7.setCellValue("登录账号");

        Cell cell8 = row.createCell(8);
        cell8.setCellStyle(cellStyle02);
        cell8.setCellValue("初始密码（VPN和OA帐号）");

        Cell cell9 = row.createCell(9);
        cell9.setCellStyle(cellStyle02);
        cell9.setCellValue("互联网邮箱");

        Cell cell10 = row.createCell(10);
        cell10.setCellStyle(cellStyle02);
        cell10.setCellValue("操作要点");

        Cell cell11 = row.createCell(11);
        cell11.setCellStyle(cellStyle02);
        cell11.setCellValue("变更日期");

        createRows(sheet, employees);

        FileOutputStream fileOut = new FileOutputStream(path + name);
        wb.write(fileOut);
        fileOut.close();


    }

    public static void createRows(Sheet sheet, List<Employee> employees) {
        for (int i = 0; i < employees.size(); i++) {
            Row row = sheet.createRow(i + 1);
            Employee e = employees.get(i);

            Cell cell = row.createCell(0);
            cell.setCellValue(nullToBlank(String.valueOf(e.getId())));

            Cell cell1 = row.createCell(1);
            cell1.setCellValue(nullToBlank(e.getEmployeeName()));

            Cell cell2 = row.createCell(2);
            cell2.setCellValue(nullToBlank(e.getEmployeeId()));

            Cell cell3 = row.createCell(3);
            cell3.setCellValue(nullToBlank(e.getDepartment()));

            Cell cell4 = row.createCell(4);
            cell4.setCellValue(nullToBlank(e.getPost()));

            Cell cell5 = row.createCell(5);
            cell5.setCellValue(nullToBlank(e.getMobilePhone()));

            Cell cell6 = row.createCell(6);
            cell6.setCellValue(nullToBlank(e.getCornopean()));

            Cell cell7 = row.createCell(7);
            cell7.setCellValue(nullToBlank(e.getUsername()));

            Cell cell8 = row.createCell(8);
            cell8.setCellValue(nullToBlank(e.getPassword()));

            Cell cell9 = row.createCell(9);
            cell9.setCellValue(nullToBlank(e.getPostcodes()));

            Cell cell10 = row.createCell(10);
            cell10.setCellValue(nullToBlank(null));

            Cell cell11 = row.createCell(11);
            cell11.setCellValue(format(e.getUpdateTime()));

        }
    }

    private static String nullToBlank(String o) {
        return o == null ? "" : o;
    }

    public static String format(Date date) {
        return date == null ? "" : new SimpleDateFormat("yyyy-MM-dd").format(date);
    }
}
